////
//
/// \file   ./src/immediate_number_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/immediate_number_node.hpp>

#include <cassert>
#include <string>

#include <boost/regex.hpp>
#include <gmpxx.h>

#include <inc/parser/parse_tree_node.hpp>

namespace inc {
namespace parser {

using std::string;
using boost::regex;
using boost::match_results;

const regex ImmediateNumberNode::imm_number_regex(
    "-?(?:(?:0(?![xbo0-7])|[1-9][0-9]*)"
    "|0x[0-9a-fA-F]+"
    "|0b[01]+"
    "|0o?[0-7]+)" );

/// \brief  Parse an immediate number value
/// \author Chandler Carruth
/// \date   2007.06.19
///
/// This parses an immediate number value. It can recognize decimal,
/// hexadecimal, and octal numbers. Classical conventions for representing
/// various based numbers are used, with decimal base being default.
/// Hexadecimal numbers are prefixed by "0x", binary by "0b", and octal numbers
/// by "0o", or for backwards compatibility simply by a leading 0 with more
/// than one digit in the number. A fractional component may be provided
/// following a "." point, and a signed integer exponent may be specified
/// following an 

bool
ImmediateNumberNode::parse( const iterator& first,
                            const iterator& last,
                            iterator& last_parsed )
{
  assert( get_text().empty() && "This node was parsed more than once!" );

  match_results<iterator> m;
  if( regex_search( first, last, m, imm_number_regex, boost::match_continuous ) )
  {
    set_text( m.str() );
    last_parsed = m.suffix().first;
    return true;
  }

  last_parsed = first;
  return false;
}

} //  end parser namespace
} //  end inc namespace
